export default class CreateHolographicLaneId {
  constructor () {
    // 创建一个对象来记录每个方位的编号和可重用的编号池
    this.counters = {
      '东': { current: 1, reusable: [] },
      '南': { current: 1, reusable: [] },
      '西': { current: 1, reusable: [] },
      '北': { current: 1, reusable: [] }
    }
    this.result = []
  }

  generateIDs (data) {
    // 遍历数据，生成ID字段
    for (let item of data) {
      let direction = item['direction']
      if (this.counters.hasOwnProperty(direction)) {
        if (this.counters[direction].reusable.length > 0) {
          // 使用可重用的编号
          item['ID'] = `${direction}-${this.counters[direction].reusable.shift()}`
        } else {
          // 使用当前的编号并递增
          item['ID'] = `${direction}-${this.counters[direction].current}`
          this.counters[direction].current++
        }
      } else {
        item['ID'] = '未知方位'
      }
    }
    console.log(this.counters)
    this.result = data
    console.log(this.result)
    return data
  }

  addItem (item) {
    let direction = item['direction']
    if (this.counters.hasOwnProperty(direction)) {
      if (this.counters[direction].reusable.length > 0) {
        // 使用可重用的编号
        item['ID'] = `${direction}-${this.counters[direction].reusable.shift()}`
      } else {
        // 使用当前的编号并递增
        item['ID'] = `${direction}-${this.counters[direction].current}`
        this.counters[direction].current++
      }
      this.result.push(item)
    } else {
      item['ID'] = '未知方位'
      this.result.push(item)
    }
    console.log('新增后结果:', this.result)
  }

  removeItem (id) {
    let index = this.result.findIndex(item => item['ID'] === id)
    if (index !== -1) {
      let direction = this.result[index]['direction']
      let num = parseInt(id.split('-')[1])
      if (this.counters.hasOwnProperty(direction)) {
        this.counters[direction].reusable.push(num)
        this.counters[direction].reusable.sort((a, b) => a - b) // 保持可重用编号池有序
      }
      this.result.splice(index, 1)
    }
    console.log('删除后结果:', this.result)
  }
}
